//
//  KSKAChartViewController.m
//  DashboardHD
//
//  Created by 楊亜玲 on 17/3/27.
//  Copyright © 2017年 Totyu. All rights reserved.
//

#import "KSKAChartViewController.h"
#import "KSKARizumuVC.h"
#import "KSKAKatsudouVC.h"

@interface KSKAChartViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
/** 集合视图cell加载了2个子控制器 */
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
/** 子控制器数组 */
@property(nonatomic, strong) NSMutableArray *controllerArray;

@property(nonatomic, strong) KSKARizumuVC *rizumuVC;

@property(nonatomic, strong) KSKAKatsudouVC *katsudouVC;

@end

@implementation KSKAChartViewController

static NSString * const reuseIdentifier = @"vcCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 注册collectionView Cell
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // 设置segmentControl
    [self.segmentControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} forState:UIControlStateNormal];
    [self.segmentControl addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];

}

// 懒加载子控制器
-(NSMutableArray *)controllerArray{
    if (!_controllerArray) {
        _controllerArray = [[NSMutableArray alloc]init];
        // 生活リズムの一覧VC
        KSKARizumuVC *vcOne = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"rizumuVC"];
        [self addChildViewController:vcOne];
        [_controllerArray addObject:vcOne];
        // 活動の集計VC
        KSKAKatsudouVC *vcTwo = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"katsudouVC"];
        [self addChildViewController:vcTwo];
        [_controllerArray addObject:vcTwo];
    }
    return _controllerArray;
}


#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.controllerArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [cell setNeedsLayout];
    [cell layoutIfNeeded];
    UIView *subView = [[_controllerArray objectAtIndex:indexPath.item]view];
    subView.size = cell.size;
    [cell.contentView addSubview:subView];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.collectionView.width,self.collectionView.height);
}

#pragma mark - 切换控制器的view

-(void)segmentClick:(UISegmentedControl *)segmentControl{
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:segmentControl.selectedSegmentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}


@end
